#include "dialog.h"
#include "ui_dialog.h"
#include<QTime>
#include<QMenu>
#include<QAction>
#include<QList>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog),
    _pool(nullptr)
{
    ui->setupUi(this);
    if(!InitWindow())
        return;
}

Dialog::~Dialog()
{
    disconnect();
    delete ui;
}

bool Dialog::InitWindow()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i=0; i<10; i++)
    {
        ui->WaitPage->addItem(QString("%1").arg(rand()%100));
    }
    QObject::connect(ui->ok_pushbutton, SIGNAL(clicked()), this, SLOT(slotOkPushbuttonOnClick()));

    ui->WaitPage->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->RunPage->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->FinishPage->setContextMenuPolicy(Qt::ActionsContextMenu);

    QList<QAction*> actionGroup;
    QAction* waitMeumDeleteAction = new QAction("delete",ui->WaitPage);
    QAction* waitMeumStartAction = new QAction("delete",ui->WaitPage);
    QAction* runMeumDeleteAction = new QAction("delete",ui->RunPage);
    QAction* finishMeumDeleteAction = new QAction("delete",ui->FinishPage);
    QAction* runMeumPauseAction = new QAction("pause",ui->RunPage);
    QAction* runMeumcontinueAction = new QAction("start",ui->RunPage);

    QObject::connect(waitMeumDeleteAction,SIGNAL(triggered()),this,SLOT(slotActionOnClickDelete()));
    QObject::connect(finishMeumDeleteAction,SIGNAL(triggered()),this,SLOT(slotActionOnClickDelete()));
    QObject::connect(runMeumDeleteAction,SIGNAL(triggered()),this,SLOT(slotActionOnClickDelete()));

    QObject::connect(waitMeumStartAction,SIGNAL(triggered()),this,SLOT(slotActionOnClickStart()));

    ui->WaitPage->addAction(waitMeumDeleteAction);
    actionGroup << runMeumcontinueAction << runMeumPauseAction<< runMeumDeleteAction;
    ui->RunPage->addActions(actionGroup);
    ui->FinishPage->addAction(finishMeumDeleteAction);

    return  true;
}

void Dialog::slotOkPushbuttonOnClick()
{
    bool ok=true;
    QString context = ui->lineEdit->text();
    context.toInt(&ok,10);
    if(ok)
    {
        ui->WaitPage->addItem(context);
    }
    ui->lineEdit->clear();
}

void Dialog::slotActionOnClickDelete()
{
    QAction *act1= qobject_cast<QAction*>(sender());
    QListWidget* act = qobject_cast<QListWidget*>(act1->parent());
    if(act && act == ui->WaitPage)
    {
        ui->WaitPage->takeItem(ui->WaitPage->currentRow());
    }
    else if (act && act == ui->RunPage)
    {
        ui->RunPage->takeItem(ui->RunPage->currentRow());
    }
    else if (act && act == ui->FinishPage)
    {
        ui->FinishPage->takeItem(ui->FinishPage->currentRow());
    }

}

void Dialog::slotActionOnClickPause()
{

}

void Dialog::slotActionOnClickStart()
{
    QAction *act1= qobject_cast<QAction*>(sender());
    QListWidget* act = qobject_cast<QListWidget*>(act1->parent());
    if(act && act == ui->WaitPage)
    {
        ui->RunPage->addItem(ui->WaitPage->currentItem()->text());
        emit emitStartATask(ui->WaitPage->currentItem()->text().toInt());
        ui->WaitPage->takeItem(ui->WaitPage->currentRow());
    }
    else if (act && act == ui->RunPage)
    {
        ui->RunPage->takeItem(ui->RunPage->currentRow());
        emit emitContinueATask(ui->WaitPage->currentItem()->text().toInt());
    }

}

void Dialog::slotAllStart()
{

}

void Dialog::sloatAllPause()
{

}

void Dialog::slotAllEnd()
{

}
